package problem108;

//108.将有序数组转换为二叉搜索树
//https://leetcode.cn/problems/convert-sorted-array-to-binary-search-tree/description/?envType=study-plan-v2&envId=top-interview-150

class Solution {
    public TreeNode sortedArrayToBST(int[] nums) {
        int n = nums.length;
        return createTree(nums, 0, n-1);
    }
    private TreeNode createTree(int[] nums, int l, int r) {
        if(l == r) return new TreeNode(nums[l]);
        if(l > r) return null;
        int m = (r+l+1)/2;
        TreeNode root = new TreeNode(nums[m]);
        root.left = createTree(nums, l, m-1);
        root.right = createTree(nums, m+1, r);
        return root;
    }
}